home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 70.8 KB | 2,535 lines | [TEXT/MPS ] |
- //----------------------------------------------------------------------------------------
- // UFileBasedDocument.cp
- // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
- //----------------------------------------------------------------------------------------
-
- #ifndef __UFILEBASEDDOCUMENT__
- #include "UFileBasedDocument.h"
- #endif
-
- // MacApp
-
- #ifndef __GEOMETRY__
- #include "Geometry.h"
- #endif
-
- #ifndef __UAPPLEEVENTS__
- #include "UAppleEvents.h"
- #endif
-
- #ifndef __UCLIPBOARDMGR__
- #include "UClipboardMgr.h"
- #endif
-
- #if qContainer
- #ifndef __UCONTAINER__
- #include "UContainer.h"
- #endif
- #endif
-
- #ifndef __UDESIGNATOR__
- #include "UDesignator.h"
- #endif
-
- #ifndef __UDISPATCHER__
- #include "UDispatcher.h"
- #endif
-
- #ifndef __UERRORMGR__
- #include "UErrorMgr.h"
- #endif
-
- #ifndef __UFILE__
- #include "UFile.h"
- #endif
-
- #ifndef __UFILESTREAM__
- #include "UFileStream.h"
- #endif
-
- #ifndef __UITERATOR__
- #include "UIterator.h"
- #endif
-
- #ifndef __ULIST__
- #include "UList.h"
- #endif
-
- #ifndef __UMACAPPGLOBALS__
- #include "UMacAppGlobals.h"
- #endif
-
- #ifndef __UMACAPPUTILITIES__
- #include "UMacAppUtilities.h"
- #endif
-
- #ifndef __UMAILER__
- // #include "UMailer.h"
- #endif
-
- #ifndef __UMEMORY__
- #include "UMemory.h"
- #endif
-
- #if !qPowerPC
- #ifndef __UPATCH__
- #include "UPatch.h"
- #endif
- #endif
-
- #ifndef __UPRINTHANDLER__
- #include "UPrintHandler.h"
- #endif
-
- #ifndef __USCRIPTING__
- #include "UScripting.h"
- #endif
-
- #ifndef __USTREAM__
- #include "UStream.h"
- #endif
-
- #ifndef __UVIEW__
- #include "UView.h"
- #endif
-
- #ifndef __UWINDOW__
- #include "UWindow.h"
- #endif
-
- // CALib
-
- #if qContainer
- #ifndef _CALIB_
- #include "CALib.h"
- #endif
- #endif
-
- // Toolbox
-
- #ifndef __AEREGISTRY__
- #include <AERegistry.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __FOLDERS__
- #include <Folders.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __STANDARDFILE__
- #include <StandardFile.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- // ANSI
-
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
-
- //========================================================================================
- // CLASS TSaveFileDocCommand
- //========================================================================================
- #undef Inherited
- #define Inherited TSaveDocCommand
-
- #pragma segment MASelCommand
- MA_DEFINE_CLASS_M1(TSaveFileDocCommand, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TSaveFileDocCommand constructor
- //----------------------------------------------------------------------------------------
- #pragma segment MASelCommand
-
- TSaveFileDocCommand::TSaveFileDocCommand()
- : fSaveFile(NULL)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TSaveFileDocCommand destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TSaveFileDocCommand::~TSaveFileDocCommand()
- {
- FreeIfObject(fSaveFile);
- }
-
- //----------------------------------------------------------------------------------------
- // TSaveFileDocCommand::ISaveDocCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment MASelCommand
-
- void TSaveFileDocCommand::ISaveFileDocCommand(CommandNumber itsCommandNumber,
- TDocument* itsDocument)
- {
- this->ISaveDocCommand(itsCommandNumber, itsDocument);
-
- TFileBasedDocument* aDocument = (TFileBasedDocument*)fDocument;
-
- // See if we already have a place to save the file
- Boolean hasFile = aDocument->FileExists();
-
- if (hasFile && fIdentifier != cSaveAs && fIdentifier != cSaveCopy)
- {
- fSaveFile = aDocument->DoMakeFile(itsCommandNumber);
- fSaveFile->SpecifyWithFile(aDocument->GetFile());
- }
- else if (!hasFile
- || itsCommandNumber == cSaveAs
- || itsCommandNumber == cSaveCopy)
- {
- if (itsCommandNumber != cSaveCopy)
- itsCommandNumber = cSaveAs;
-
- fSaveFile = aDocument->DoMakeFile(itsCommandNumber);
-
- // Ask the user for the destination file location and name.
- FailOSErr(MAInteractWithUser());
- FailInfo fi2;
- Try(fi2)
- {
- // If this fails (i.e. user cancels) then we want it to be caught
- // in elsewhere with a more appropriate error code.
- aDocument->RequestFileName(itsCommandNumber, FALSE, fSaveFile);
- fi2.Success();
- }
- else // Recover
- {
- if (fi2.error == noErr && fi2.message == messageCancelled)
- FailOSErr(userCanceledErr);
- fi2.ReSignal();
- }
- }
-
- fIdentifier = itsCommandNumber;
- }
-
- //----------------------------------------------------------------------------------------
- // TSaveFileDocCommand::ISaveDocCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment MASelCommand
-
- void TSaveFileDocCommand::ISaveFileDocCommand(TDocument* itsDocument,
- TAppleEvent* message,
- TAppleEvent* reply)
- {
- FailInfo fi;
- Try(fi)
- {
- this->ISaveDocCommand(itsDocument, message, reply);
-
- TFileBasedDocument* aDocument = (TFileBasedDocument*)fDocument;
-
- if (message->HasParameter(keyAESaveCopy) && message->ReadBoolean(keyAESaveCopy))
- fIdentifier = cSaveCopy;
-
- // Determine where to save the file from the AppleEvent message.
- if (message->HasParameter(keyAEFile))
- {
- DescType returnedType;
- FSSpec theFSSpec;
- Size actualSize;
-
- FailOSErr(AEGetParamPtr(&message->fMessage, keyAEFile, typeFSS, &returnedType, (Ptr) & theFSSpec, sizeof(theFSSpec), &actualSize));
- fSaveFile = aDocument->DoMakeFile(fIdentifier);
- fSaveFile->Specify(theFSSpec);
- fIdentifier = cSaveAs;
- }
- else
- {
- // No but do we already have a file wherein we can save?
- TFileBasedDocument* aDocument = (TFileBasedDocument*)fDocument;
- Boolean hasFile = aDocument->FileExists();
-
- if (hasFile)
- {
- fSaveFile = aDocument->DoMakeFile(fIdentifier);
- fSaveFile->SpecifyWithFile(aDocument->GetFile());
- }
- else
- {
- fIdentifier = cSaveAs;
-
- fSaveFile = aDocument->DoMakeFile(fIdentifier);
-
- // Ask the user for the destination file location and name.
- FailOSErr(MAInteractWithUser());
- FailInfo fi2;
- Try(fi2)
- {
- // If this fails (i.e. user cancels) then we want it to be caught
- // in elsewhere with a more appropriate error code.
- aDocument->RequestFileName(fIdentifier, FALSE, fSaveFile);
- fi2.Success();
- }
- else // Recover
- {
- if (fi2.error == noErr && fi2.message == messageCancelled)
- FailOSErr(userCanceledErr);
- fi2.ReSignal();
- }
- }
- }
- fi.Success();
- }
- else // Recover
- {
- // If this fails because user cancels then we want it
- // to be caught with a more appropriate error code.
- if (fi.error == noErr && fi.message == messageCancelled)
- fi.error = userCanceledErr;
- //this->SetValidationError(fi.error); // In case we are linked.
- fi.ReSignal();
- }
-
- if (message->HasParameter(keyAEFileType))
- fSaveFile->fFileType = message->ReadType(keyAEFileType);
- }
-
- //----------------------------------------------------------------------------------------
- // TSaveFileDocCommand::MakeAppleEvent:
- //----------------------------------------------------------------------------------------
- #pragma segment MACommandRes
-
- TAppleEvent* TSaveFileDocCommand::MakeAppleEvent()
- {
- // We have to save the file specifier in the AppleEvent.
- MAVolatileInit(TAppleEvent*, theEvent, NULL);
- CTempDesc theDocDesc;
-
- theEvent = Inherited::MakeAppleEvent();
-
- FailInfo fi;
- Try(fi)
- {
- // Which file to store it into.
- if (fSavingState != kDontSave && fSaveFile != NULL)
- {
- CAEDesc aSaveFileDesc;
- FSSpec theFileSpec;
- fSaveFile->GetFileSpec(theFileSpec);
- aSaveFileDesc.PutFSSpec(theFileSpec);
- theEvent->WriteParameter(keyAEFile, aSaveFileDesc);
- }
-
- fi.Success();
- }
- else // Recover
- {
- theEvent = (TAppleEvent *)FreeIfObject(theEvent);
- fi.ReSignal();
- }
- return theEvent;
- }
-
- //----------------------------------------------------------------------------------------
- // TSaveFileDocCommand::DoIt:
- //----------------------------------------------------------------------------------------
- #pragma segment MAClose
-
- void TSaveFileDocCommand::DoIt()
- {
- if (fDocument && fSavingState != kDontSave)
- {
- // FailInfo fi;
- // Try(fi)
- // {
- Boolean makingCopy = (fIdentifier == cSaveCopy);
-
- if (fDocument->fCommitOnSave || (!makingCopy))
- fDocument->CommitLastCommand();
-
- // Set the document's TFile object so that it saves in the correct place.
- TFileBasedDocument* aFileDocument = (TFileBasedDocument*)fDocument;
- MAVolatileInit(TFile*, existingFileCopy, NULL);
- existingFileCopy = aFileDocument->DoMakeFile(fIdentifier);
-
- FailInfo fi2;
- Try(fi2)
- {
- existingFileCopy->SpecifyWithFile(aFileDocument->GetFile());
- aFileDocument->GetFile()->SpecifyWithFile(fSaveFile);
- Boolean currFileExists = aFileDocument->fFileExists;
- // Test to see if the destination file is there.
- aFileDocument->fFileExists = (fSaveFile->GetModificationDate() != 0);
-
- aFileDocument->SaveDocument(cSave);
-
- if (fIdentifier == cSaveCopy)
- {
- // Restore the existing file info since we just made a copy.
- aFileDocument->fFileExists = currFileExists;
- aFileDocument->GetFile()->SpecifyWithFile(existingFileCopy);
- }
- FreeIfObject(existingFileCopy);
- fi2.Success();
- }
- else // Recover
- {
- FreeIfObject(existingFileCopy);
- fi2.ReSignal();
- }
- // fi.Success();
- // }
- // else
- // {
- // this->SetValidationError(fi.error); // In case we are linked.
- // fi.ReSignal();
- // }
- }
- }
-
-
- //========================================================================================
- // CLASS TCloseFileDocCommand
- //========================================================================================
- #undef Inherited
- #define Inherited TCloseDocCommand
-
- #pragma segment MASelCommand
- MA_DEFINE_CLASS_M1(TCloseFileDocCommand, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TCloseFileDocCommand constructor
- //----------------------------------------------------------------------------------------
- #pragma segment MASelCommand
-
- TCloseFileDocCommand::TCloseFileDocCommand()
- {
- fCloseFile = NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // TCloseFileDocCommand destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TCloseFileDocCommand::~TCloseFileDocCommand()
- {
- fCloseFile = (TFile*) FreeIfObject(fCloseFile);
- }
-
- //----------------------------------------------------------------------------------------
- // TCloseFileDocCommand::ICloseDocCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment MASelCommand
-
- void TCloseFileDocCommand::ICloseFileDocCommand(CommandNumber itsCommandNumber,
- TDocument* itsDocument)
- {
- this->ICloseDocCommand(itsCommandNumber, itsDocument);
-
- fSaveCommand = cSave;
-
- // Are we saving?
- if (fSavingState == kDoSave)
- {
- FailInfo fi;
- Try(fi)
- {
- FailOSErr(MAInteractWithUser());
- TFileBasedDocument* aDocument = (TFileBasedDocument*)fDocument;
- Boolean hasFile = aDocument->FileExists();
-
- // Get where to save the file
-
- if (hasFile)
- {
- fCloseFile = aDocument->DoMakeFile(itsCommandNumber);
- fCloseFile->SpecifyWithFile(aDocument->GetFile());
- }
- else
- {
- // We have to ask for the save location.
- fSaveCommand = cSaveAs;
-
- fCloseFile = aDocument->DoMakeFile(cSaveAs);
-
- // Ask the user for the destination file location and name.
- aDocument->RequestFileName(cSaveAs, FALSE, fCloseFile);
- }
- fi.Success();
- }
- else // Recover
- {
- // If this fails because user cancels then we want it
- // to be caught with a more appropriate error code.
- if (fi.error == noErr && fi.message == messageCancelled)
- fi.error = userCanceledErr;
- //this->SetValidationError(fi.error); // In case we are linked.
- fi.ReSignal();
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TCloseFileDocCommand::ICloseFileDocCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment MASelCommand
-
- void TCloseFileDocCommand::ICloseFileDocCommand(TDocument* itsDocument,
- TAppleEvent* message,
- TAppleEvent* reply)
- {
- this->ICloseDocCommand(itsDocument, message, reply);
-
- fSaveCommand = cSave;
-
- if (fSavingState != kDontSave)
- {
- FailInfo fi;
- Try(fi)
- {
- // Does the AppleEvent specifically say where to save the file;
- if (message->HasParameter(keyAEFile))
- {
- DescType returnedType;
- FSSpec theFSSpec;
- Size actualSize;
-
- FailOSErr(AEGetParamPtr(&message->fMessage, keyAEFile, typeFSS, &returnedType, (Ptr) & theFSSpec, sizeof(theFSSpec), &actualSize));
- TFileBasedDocument* aDocument = (TFileBasedDocument*)fDocument;
- fCloseFile = aDocument->DoMakeFile(fIdentifier);
- fCloseFile->Specify(theFSSpec);
- fSaveCommand = cSaveAs;
- }
- else
- {
- // No but do we already have a file wherein we can save?
- TFileBasedDocument* aDocument = (TFileBasedDocument*)fDocument;
- Boolean hasFile = aDocument->FileExists();
-
- // Get where to save the file
-
- if (hasFile)
- {
- fCloseFile = aDocument->DoMakeFile(cClose);
- fCloseFile->SpecifyWithFile(aDocument->GetFile());
- }
- else
- {
- fSaveCommand = cSaveAs;
- fCloseFile = aDocument->DoMakeFile(cSaveAs);
-
- // Ask the user for the destination file location and name.
- FailOSErr(MAInteractWithUser());
- aDocument->RequestFileName(cSaveAs, FALSE, fCloseFile);
- }
- }
- fi.Success();
- }
- else // Recover
- {
- // If this fails because user cancels then we want it
- // to be caught with a more appropriate error code.
- if (fi.error == noErr && fi.message == messageCancelled)
- fi.error = userCanceledErr;
- //this->SetValidationError(fi.error); // In case we are linked.
- fi.ReSignal();
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TCloseFileDocCommand::MakeAppleEvent:
- //----------------------------------------------------------------------------------------
- #pragma segment MACommandRes
-
- TAppleEvent* TCloseFileDocCommand::MakeAppleEvent()
- {
- // We have to save the file specifier in the AppleEvent.
- MAVolatileInit(TAppleEvent*, theEvent, NULL);
- CTempDesc theDocDesc;
-
- theEvent = Inherited::MakeAppleEvent();
-
- FailInfo fi;
- Try(fi)
- {
- // Which file to store it into.
- if (fSavingState != kDontSave && fCloseFile != NULL)
- {
- CAEDesc aCloseFileDesc;
- FSSpec theFileSpec;
- fCloseFile->GetFileSpec(theFileSpec);
- aCloseFileDesc.PutFSSpec(theFileSpec);
- theEvent->WriteParameter(keyAEFile, aCloseFileDesc);
- }
- fi.Success();
- }
- else // Recover
- {
- theEvent = (TAppleEvent *)FreeIfObject(theEvent);
- fi.ReSignal();
- }
- return theEvent;
- }
-
- //----------------------------------------------------------------------------------------
- // TCloseFileDocCommand::DoIt:
- //----------------------------------------------------------------------------------------
- #pragma segment MAClose
-
- void TCloseFileDocCommand::DoIt()
- {
- if (fDocument)
- {
- // FailInfo fi;
- // Try(fi)
- // {
- // Some documents want us to specifically ask if they should be saved.
- if (fSavingState == kAskSave)
- {
- short poseResult = fDocument->PoseSaveDialog();
- switch (poseResult)
- {
- case kNoButton:
- fSavingState = kDontSave;
- break;
- case kYesButton:
- fSavingState = kDoSave;
- break;
- case cancel:
- FailOSErr(userCanceledErr);
- break;
- }
- }
-
- if (fSavingState == kDoSave)
- {
- fDocument->CommitLastCommand();
-
- // Set the document's TFile object so that it saves in the correct place.
- TFileBasedDocument* aFileDocument = (TFileBasedDocument*)fDocument;
- MAVolatileInit(TFile*, existingFileCopy, NULL);
- existingFileCopy = aFileDocument->DoMakeFile(fSaveCommand);
-
- FailInfo fi3;
- Try(fi3)
- {
- existingFileCopy->SpecifyWithFile(aFileDocument->GetFile());
- aFileDocument->GetFile()->SpecifyWithFile(fCloseFile);
- Boolean currFileExists = aFileDocument->fFileExists;
- aFileDocument->fFileExists = (fCloseFile->GetModificationDate() != 0);
-
- aFileDocument->SaveDocument(cSave);
-
- FreeIfObject(existingFileCopy);
- fi3.Success();
- }
- else // Recover
- {
- FreeIfObject(existingFileCopy);
- fi3.ReSignal();
- }
- }
-
- // If there is actually some saving to do it should have already been done.
- fDocument->SetChangeCount(0);
- fDocument->CloseAndFree();
- fDocument = NULL;
- // fi.Success();
- // }
- // else
- // {
- // this->SetValidationError(fi.error); // In case we are linked.
- // fi.ReSignal();
- // }
- }
- }
-
- //========================================================================================
- // CLASS TFileBasedDocument
- //========================================================================================
- #undef Inherited
- #define Inherited TDocument
-
- #pragma segment MAOpen
- MA_DEFINE_CLASS_M1(TFileBasedDocument, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument constructor
- //----------------------------------------------------------------------------------------
- #pragma segment MAOpen
-
- TFileBasedDocument::TFileBasedDocument() :
- #if qContainer
- fContainerDocument(NULL),
- fContainerOffset(0),
- fContainerLength(0),
- fRootWindowRef(NULL),
- #endif
- fFile(NULL),
- fFileExists(FALSE)
- {
- fHowToSave = svtAskUser;
- fScrapType = ' '; // need constant kNoScrapType
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::IFileBasedDocument:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOpen
-
- void TFileBasedDocument::IFileBasedDocument(TFile* itsFile, OSType itsScrapType)
- {
- this->IDocument();
-
- fFile = itsFile;
- fScrapType = itsScrapType;
-
- FailInfo fi;
- Try(fi)
- {
- #if qContainer
- gDispatcher->AddCADocument(this); //add this to the App's list of ContainerDoc's
- fODPartViewList = NewList(); //set up the ODView listing
- fODPartViewList->IList();
- #endif
- if (!fFile)
- fFile = this->DoMakeFile(cNew);
- fi.Success();
- }
- else // Recover
- {
- this->Free();
- fi.ReSignal();
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::DoMakeFile:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOpen
-
- TFile* TFileBasedDocument::DoMakeFile(CommandNumber itsCommandNumber)
- {
- return gDispatcher->DoMakeFile(itsCommandNumber);
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::DoMakeFileHandler:
- //----------------------------------------------------------------------------------------
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::Free:
- //----------------------------------------------------------------------------------------
- #pragma segment MAClose
-
- TFileBasedDocument::~TFileBasedDocument()
- {
- #if qContainer
- if (fContainerDocument)
- {
- gDispatcher->DeleteCADocument(this); //delete this from the App's list of ContainerDoc's
- CACloseDocument(fContainerDocument);
- fContainerDocument = NULL;
- }
- #endif
- if (fFile)
- {
- this->CloseFile();
- fFile->Free();
- fFile = NULL;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::DoInitialState:
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- void TFileBasedDocument::DoInitialState()
- {
- // don't need to call Inherited method because it is empty
-
- #if qContainer
- if (gContainerLib)
- {
- // ••• MDR - The IFileBasedDocument method creates a file with an
- // empty file spec so the following test is always try. But since its file
- // spec doesn't identify a valid file the CACreateDocument fails. I've temporarily
- // forced the test in order to force a file spec to be initialized.
- FSSpec fileSpec;
- if (FALSE && fFile) // ••• MDR - Note the FALSE
- fileSpec = fFile->fFileSpec;
- else
- {
- CStr15("\pUntitled").CopyTo(fileSpec.name);
- // ••• MDR - Changed to use the temporary items folder rather than the desktop folder.
- FailOSErr(FindFolder(kOnSystemDisk, kTemporaryFolderType, kCreateFolder, &fileSpec.vRefNum, &fileSpec.parID));
-
- MakeDatedUniqueFSSpec(&fileSpec);
-
- SetTitle(fileSpec.name);
- }
- fContainerDocument = CACreateDocument(&fileSpec, NULL);
- if (fContainerDocument)
- {
- CASetDocumentKind(fContainerDocument, 0 /* kDrawShapesDocumentKind */);
- this->RegisterCAHandlers();
- }
- // ••• MDR - Need to determine the document kind by calling a member function.
- }
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::FindDocument:
- //----------------------------------------------------------------------------------------
- #pragma segment MAFile
-
- Boolean TFileBasedDocument::FindDocument(TFile* aFile)// override
- {
- return FileAlreadyOpen(aFile);
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::CheckFile:
- //----------------------------------------------------------------------------------------
- #pragma segment MAFile
-
- void TFileBasedDocument::CheckFile(ResNumber rsrcId,
- short rsrcIndex,
- Boolean reverting)
- {
- OSErr err = FileChanged(reverting); // don't care about the file type if saving
- if (err == errFileChanged)
- {
- FailOSErr(MAInteractWithUser());
- CStr255 s;
- GetIndString(s, rsrcId, rsrcIndex);
- ParamText(fTitle, s, gEmptyString, gEmptyString);
- if (MacAppAlert(phFileChanged, NULL) == cancel)
- Failure(noErr, messageCancelled);
- }
- else if ((err != noErr) && reverting)
- Failure(err, 0);
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::Close:
- //----------------------------------------------------------------------------------------
- #pragma segment MAClose
-
- void TFileBasedDocument::Close()
- {
- long changeCount;
- short poseResult = kYesButton;
-
- #if qDebug
- if (gClipboardMgr->fClipWindow->fDocument == this)
- ProgramBreak("Attempt to close clipboard document");
- #endif
-
- changeCount = this->GetChangeCount();
- if (changeCount && fAskOnClose)
- {
- poseResult = this->PoseSaveDialog();
- if (poseResult == cancel)
- Failure(noErr, messageCancelled);
- }
-
- if (changeCount)
- {
- if (poseResult == kYesButton)
- this->SaveDocument(cClose); // Will fail if unable to save
- else if (poseResult == kNoButton)
- this->Abandon();
- }
- Inherited::Close();
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::DoAESave
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- void TFileBasedDocument::DoAESave(TAppleEvent* message,
- TAppleEvent* reply)
- {
- // Generate a save document command.
- Boolean oldTempAlloc = TemporaryAllocation(TRUE);
- Boolean oldObjectPerm = AllocateObjectsFromPerm(FALSE);
- TSaveFileDocCommand* aSaveFileDocCommand = NULL;
-
- aSaveFileDocCommand = new TSaveFileDocCommand();
- aSaveFileDocCommand->ISaveFileDocCommand(this, message, reply);
-
- // Immediately process the command rather than post it.
- aSaveFileDocCommand->Process();
-
- TemporaryAllocation(oldTempAlloc);
- AllocateObjectsFromPerm(oldObjectPerm);
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::DoAEClose:
- //----------------------------------------------------------------------------------------
- #pragma segment MAScriptingRes
-
- void TFileBasedDocument::DoAEClose(TAppleEvent* message,
- TAppleEvent* reply)
- {
- Boolean oldTempAlloc = TemporaryAllocation(TRUE);
- Boolean oldObjectPerm = AllocateObjectsFromPerm(FALSE);
- TCloseFileDocCommand* aCloseFileDocCommand = NULL;
-
- aCloseFileDocCommand = new TCloseFileDocCommand;
- aCloseFileDocCommand->ICloseFileDocCommand(this, message, reply);
-
- // Immediately process the command rather than post it.
- aCloseFileDocCommand->Process();
-
- TemporaryAllocation(oldTempAlloc);
- AllocateObjectsFromPerm(oldObjectPerm);
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::DoNeedDiskSpace:
- //----------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- void TFileBasedDocument::DoNeedDiskSpace(TFile* itsFile,
- long& dataForkBytes,
- long& rsrcForkBytes)
- {
- if (itsFile->HasRsrcFork())
- {
- CStr255 theName;
- CStr255 theMessage;
-
- gDispatcher->GetApplicationName(theName);
- this->GetFinderMessage(theMessage);
-
- if (!theMessage.IsEmpty())
- rsrcForkBytes += kRsrcTypeOverhead + kRsrcOverhead + theMessage.Length() + 1;
- else if (!theName.IsEmpty())
- rsrcForkBytes += kRsrcTypeOverhead + kRsrcOverhead + theName.Length() + 1;
-
- if (fSavePrintInfo && fPrintInfo)
- rsrcForkBytes += kRsrcTypeOverhead + kRsrcOverhead + TPrintHandler::gPrintHandler->GetSavePrintInfoSize(fPrintInfo, itsFile, itsFile->HasRsrcFork());
-
- if (fSaveUserSelection)
- {
- TDesignator* theSelection = this->GetUserSelection();
- if (theSelection)
- {
- TCountingStream* aStream;
- aStream = new TCountingStream;
- aStream->ICountingStream();
- aStream->WriteStreamObject(theSelection);
- rsrcForkBytes += kRsrcTypeOverhead + kRsrcOverhead + aStream->GetSize();
- aStream->Free();
- }
- }
- rsrcForkBytes += kRsrcFileOverhead;
- }
- else if (fSavePrintInfo && fPrintInfo)
- dataForkBytes += TPrintHandler::gPrintHandler->GetSavePrintInfoSize(fPrintInfo, itsFile, itsFile->HasRsrcFork());
-
- #if qContainer
- dataForkBytes += sizeof(CAOffset) + sizeof(CASize);
-
- if (gContainerLib && fContainerDocument)
- dataForkBytes += fContainerLength;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::RegainControl:
- //----------------------------------------------------------------------------------------
- #pragma segment MADocumentRes
-
- void TFileBasedDocument::RegainControl()
- {
- if (ValidateFileSpec())
- {
- CStr63 fileName;
- GetFileName(fileName);
- SetTitle(fileName);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::DoRead:
- //----------------------------------------------------------------------------------------
- #pragma segment MAReadFile
-
- void TFileBasedDocument::DoRead(TFile* aFile,
- Boolean forPrinting)
- {
- #if qContainer
- // ••• MDR
- // Always place the container information at the beginning of the file
- // so that the embedded objects can be read even if the rest of the document
- // cannot be understood.
- TFileStream* aFileStream = new TFileStream;
- aFileStream->IFileStream(aFile);
-
- aFileStream->ReadBytes(&fContainerOffset, sizeof(CAOffset));
- aFileStream->ReadBytes(&fContainerLength, sizeof(CASize));
-
- // Now stream in the embedded parts.
- if (gContainerLib && (fContainerLength > 0))
- {
- FSSpec theFileSpec;
- aFile->GetFileSpec(theFileSpec);
- fContainerDocument = CAOpenDocument(&theFileSpec, NULL, fContainerOffset, fContainerLength);
-
- // ••• MDR - Don't we need to close the document here?
-
- aFileStream->SetPosition(fContainerOffset + fContainerLength);
-
- // ••• MDR - I don't really like doing the registration with CALib
- // here but this seems to be the bottleneck. Review this.
-
- }
- FreeIfObject(aFileStream);
- #endif
-
- if (fSavePrintInfo)
- this->DoReadPrintInfo(aFile, forPrinting);
-
- if (fSaveUserSelection)
- this->DoReadSelection(aFile, forPrinting);
-
- #if qAttachable
- if (fSaveAttachedScript)
- this->DoReadScript(aFile, forPrinting);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::DoReadPrintInfo:
- //----------------------------------------------------------------------------------------
- #pragma segment MAReadFile
-
- void TFileBasedDocument::DoReadPrintInfo(TFile* aFile,
- Boolean /* forPrinting */)
- {
- if (FileExists())
- fPrintInfo = TPrintHandler::gPrintHandler->RestorePrintInfo(aFile, aFile->HasRsrcFork());
- }
-
- #if qAttachable
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::DoReadScript:
- //----------------------------------------------------------------------------------------
- #pragma segment MAReadFile
-
- void TFileBasedDocument::DoReadScript(TFile* aFile,
- Boolean /* forPrinting */)
- {
- Handle scriptInfo = NULL;
-
- if (HasAppleScript() && FileExists())
- {
- if (aFile->HasRsrcFork())
- {
- if (aFile->IsRsrcForkOpen())
- {
- CTempDesc scriptDesc;
-
- scriptDesc.ReadFrom(aFile, kScriptInfoRsrcType, kScriptInfoRsrcID);
- if (scriptDesc.fAEDesc.descriptorType != typeNull)
- this->SetOSAScript(scriptDesc);
- }
- }
- else
- {
- MAVolatileInit(TFileStream*, aFileStream, new TFileStream);
- aFileStream->IFileStream(aFile);
-
- FailInfo fi;
- Try(fi)
- {
- this->ReadOSAScript(aFileStream);
- fi.Success();
- }
- else
- {
- aFileStream = (TFileStream *)FreeIfObject(aFileStream);
- fi.ReSignal();
- }
-
- aFileStream = (TFileStream *)FreeIfObject(aFileStream);
- }
- }
- }
-
- #endif // qAttachable
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::DoReadSelection:
- //----------------------------------------------------------------------------------------
- #pragma segment MAReadFile
-
- void TFileBasedDocument::DoReadSelection(TFile* aFile,
- Boolean forPrinting)
- {
- MAVolatileInit(Handle, theSelectionRsrc, NULL);
- if (FileExists() && aFile->HasRsrcFork() && aFile->IsRsrcForkOpen())
- {
- short saveRefNum = aFile->UseResource();
- if (ResError() == noErr)
- theSelectionRsrc = Get1Resource(kDesignatorResType, kSelectionRsrcID);
- MAUseResFile(saveRefNum);
- }
-
- if (theSelectionRsrc && !forPrinting)
- {
- TObject* aUserSelection = NULL; // No need to be volatile: it is passed by reference
-
- // read in the associated designator
- MAVolatileInit(THandleStream*, aHandleStream, new THandleStream);
- aHandleStream->IHandleStream(theSelectionRsrc, 10);
-
- FailInfo fi;
- Try(fi)
- {
- if (aHandleStream->ReadStreamObject(aUserSelection))
- this->SetUserSelection((TDesignator*) aUserSelection);
- #if qDebugMsg
- else
- {
- ProgramBreak("###Unknown TDesignator for selection.");
- }
- #endif
-
- aHandleStream = (THandleStream*) FreeIfObject(aHandleStream);
- ReleaseResource(theSelectionRsrc);
-
- fi.Success();
- }
- else // Recover
- {
- aUserSelection = FreeIfObject(aUserSelection);
- aHandleStream = (THandleStream*) FreeIfObject(aHandleStream);
- ReleaseResource(theSelectionRsrc);
-
- fi.ReSignal();
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::AboutToSaveFile:
- //----------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- void TFileBasedDocument::AboutToSaveFile(TFile* /* theSaveFile */,
- CommandNumber /* itsCmd */,
- Boolean& /* makingCopy */)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::DoWrite:
- //----------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- void TFileBasedDocument::DoWrite(TFile* aFile,
- Boolean makingCopy)
- {
- this->DoWriteFinderString(aFile, makingCopy);
-
- #if qContainer
- TFileStream* aFileStream = new TFileStream;
- aFileStream->IFileStream(aFile);
-
- fContainerOffset = aFileStream->GetSize() + sizeof(CAOffset) + sizeof(CASize);
- fContainerLength = 0;
-
- aFileStream->WriteBytes(&fContainerOffset, sizeof(CAOffset));
- aFileStream->WriteBytes(&fContainerLength, sizeof(CASize));
-
- if (gContainerLib && fContainerDocument)
- {
- FSSpec newFileSpec;
- aFile->GetFileSpec(newFileSpec);
- CASaveDocument(fContainerDocument, &newFileSpec, NULL, fContainerOffset, &fContainerLength);
- }
-
- if (fContainerLength > 0)
- {
- aFileStream->SetPosition(fContainerOffset - sizeof(CASize));
- aFileStream->WriteBytes(&fContainerLength, sizeof(CASize));
- aFileStream->SetPosition(fContainerOffset + fContainerLength);
- }
-
- aFileStream = (TFileStream *)FreeIfObject(aFileStream);
- #endif
-
- if (fSavePrintInfo && fPrintInfo)
- this->DoWritePrintInfo(aFile, makingCopy);
-
- if (fSaveUserSelection)
- this->DoWriteSelection(aFile, makingCopy);
-
- #if qAttachable
- if (fSaveAttachedScript)
- this->DoWriteScript(aFile, makingCopy);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::DoWriteFinderString:
- //----------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- void TFileBasedDocument::DoWriteFinderString(TFile* aFile,
- Boolean /* makingCopy */)
- {
- if (aFile->HasRsrcFork())
- {
- CStr255 theName;
- CStr255 theMessage;
-
- gDispatcher->GetApplicationName(theName);
- this->GetFinderMessage(theMessage);
-
- if (!theMessage.IsEmpty())
- {
- MAVolatile(CStringHandle, messageHandle);
-
- Boolean oldPerm = PermAllocation(TRUE);
- messageHandle = (CStringHandle)NewString(theMessage);
- PermAllocation(oldPerm);
- FailNIL((Handle) messageHandle);
-
- FailInfo fi;
- Try(fi)
- {
- MAAddResource((Handle) messageHandle, kFinderStringType, kFinderMessageID, gEmptyString);
- FailResError();
- fi.Success();
- }
- else
- {
- messageHandle = (CStringHandle) DisposeIfHandle((Handle) messageHandle);
- fi.ReSignal();
- }
- }
- else if (!theName.IsEmpty())
- {
- MAVolatile(CStringHandle, nameHandle);
-
- Boolean oldPerm = PermAllocation(TRUE);
- nameHandle = (CStringHandle)NewString(theName);
- PermAllocation(oldPerm);
- FailNIL((Handle) nameHandle);
-
- FailInfo fi;
- Try(fi)
- {
- MAAddResource((Handle) nameHandle, kFinderStringType, kFinderApplicationNameID, gEmptyString);
- FailResError();
- fi.Success();
- }
- else
- {
- nameHandle = (CStringHandle) DisposeIfHandle((Handle) nameHandle);
- fi.ReSignal();
- }
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::DoWritePrintInfo:
- //----------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- void TFileBasedDocument::DoWritePrintInfo(TFile* aFile,
- Boolean /* makingCopy */)
- {
- if (fPrintInfo)
- TPrintHandler::gPrintHandler->SavePrintInfo(fPrintInfo, aFile, aFile->HasRsrcFork());
- #if qDebug
- else
- ProgramBreak("No print record in document!");
- #endif
- }
-
- #if qAttachable
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::DoWriteScript:
- //----------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- void TFileBasedDocument::DoWriteScript(TFile* aFile,
- Boolean /* makingCopy */)
- {
- if (this->HasOSAScript())
- {
- if (aFile->HasRsrcFork())
- {
- CStr255 resourceName;
- fOSAScript->WriteOSAScript(aFile, kScriptInfoRsrcType, kScriptInfoRsrcID, resourceName);
- }
- else
- {
- MAVolatileInit(TFileStream*, aFileStream, new TFileStream);
- aFileStream->IFileStream(aFile);
-
- FailInfo fi;
- Try(fi)
- {
- this->WriteOSAScript(aFileStream, typeOSAGenericStorage);
- fi.Success();
- }
- else
- {
- aFileStream = (TFileStream *)FreeIfObject(aFileStream);
- fi.ReSignal();
- }
- aFileStream = (TFileStream *)FreeIfObject(aFileStream);
- }
- }
- }
-
- #endif // qAttachable
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::DoWriteSelection:
- //----------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- void TFileBasedDocument::DoWriteSelection(TFile* aFile,
- Boolean /* makingCopy */)
- {
- TDesignator* theSelection = this->GetUserSelection();
- if (theSelection && aFile->HasRsrcFork())
- {
- // set up for failure handler
- MAVolatileInit(Handle, theSelectionRsrc, NULL);
-
- // allocate a handle for the resource
- theSelectionRsrc = NewPermHandle(50); //!!! SRF - what's this magic number?
-
- // write the user selection to the handle
- FailInfo fi;
- Try(fi)
- {
- MAVolatileInit(THandleStream*, aHandleStream, new THandleStream);
- aHandleStream->IHandleStream(theSelectionRsrc, 10);
-
- FailInfo innerfi;
- Try(innerfi)
- {
- // write out the associated designator
- aHandleStream->WriteStreamObject(theSelection);
- aHandleStream->Free();
-
- innerfi.Success();
- }
- else
- {
- aHandleStream = (THandleStream*) FreeIfObject(aHandleStream);
-
- innerfi.ReSignal();
- }
-
- // add the new selection resource
- MAAddResource(theSelectionRsrc, kDesignatorResType, kSelectionRsrcID, gEmptyString);
- FailResError();
-
- fi.Success();
- }
- else // Recover
- {
- theSelectionRsrc = DisposeIfHandle(theSelectionRsrc);
-
- fi.ReSignal();
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::CloseFile:
- //----------------------------------------------------------------------------------------
- #pragma segment MADocumentRes
-
- void TFileBasedDocument::CloseFile()
- {
- if (fFile)
- fFile->CloseFile();
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::ReadDocument:
- //----------------------------------------------------------------------------------------
- #pragma segment MAReadFile
-
- void TFileBasedDocument::ReadDocument(Boolean forPrinting) // override
- {
- CStr63 fileName;
- GetFileName(fileName);
- fTitle = fileName;
- ReadFile(forPrinting);
- this->SetChangeCount(0);
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::ReadStationery:
- //----------------------------------------------------------------------------------------
- #pragma segment MAReadFile
-
- void TFileBasedDocument::ReadStationery(TFile* itsNewFile) // override
- {
- ReadFile(kForDisplay);
- SetFile(itsNewFile);
- this->SetChangeCount(1);
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::RevertDocument:
- //----------------------------------------------------------------------------------------
- #pragma segment MAReadFile
-
- void TFileBasedDocument::RevertDocument()
- {
- FailInfo fi;
- Try(fi)
- {
- this->CheckFile(kIDBuzzString, bzRevertAnyways, TRUE);
-
- this->CommitLastCommand();
-
- this->FreeData();
-
- if (FileExists())
- ReadFile(kForDisplay);
- else
- {
- CObjectIterator iter(fViewList);
- TPrintHandler* itsPrintHandler;
-
- for (TView* view = (TView*) iter.FirstObject(); iter.More(); view = (TView*) iter.NextObject())
- if ((itsPrintHandler = view->GetPrintHandler()) != NULL)
- itsPrintHandler->Reset();
-
- this->DoInitialState();
- }
-
- this->SetChangeCount(0);
- fi.Success();
- }
- else // Recover
- {
- this->Free(); // Free the document since the data
- // may not be in any state to redisplay
- if (fi.error == fnfErr)
- fi.error = errRevertFNF;
-
- if (fi.message == 0)
- gErrorParm3 = fTitle;
-
- FailNewMessage(fi.error, fi.message, messageRevertFailed);
- }
- }
-
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::GetSaveLocation
- //----------------------------------------------------------------------------------------
- #pragma segment MASelCommand
-
- void TFileBasedDocument::GetSaveLocation(CommandNumber itsCommandNumber, CAEDesc& theSaveDesc)
- // Creates an CAEDesc describing where the document would be saved
- {
- Boolean askForFilename = !FileExists() || ((itsCommandNumber != cSave) && (itsCommandNumber != cClose));
- Boolean makingCopy = itsCommandNumber == cSaveCopy;
- TFile* theSaveFile = NULL;
- theSaveFile = this->DoMakeFile(itsCommandNumber);
- if (askForFilename)
- RequestFileName(itsCommandNumber, makingCopy, theSaveFile);
- else
- theSaveFile->SpecifyWithFile(GetFile());
- FailOSErr(AECreateDesc(typeFSS,(Ptr)&theSaveFile->fFileSpec,sizeof(FSSpec), theSaveDesc));
- FreeIfObject(theSaveFile);
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::IsChanged:
- //----------------------------------------------------------------------------------------
- #pragma segment MADocumentRes
-
- Boolean TFileBasedDocument::IsChanged()
- {
- #if qContainer
- return (Inherited::IsChanged()
- || (gContainerLib
- && this->GetContainer()
- && CAHasDocumentChanged(fContainerDocument)));
- #else
- return Inherited::IsChanged();
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::DoSave
- //----------------------------------------------------------------------------------------
- #pragma segment MASelCommand
-
- void TFileBasedDocument::DoSave(CommandNumber itsCommandNumber)
- {
- // Generate a save document command.
- MAVolatileInit(Boolean, oldTempAlloc, TemporaryAllocation(TRUE));
- MAVolatileInit(Boolean, oldObjectPerm, AllocateObjectsFromPerm(FALSE));
- MAVolatileInit(TSaveFileDocCommand*, aSaveFileDocCommand, NULL);
-
- FailInfo fi;
- Try(fi)
- {
- aSaveFileDocCommand = new TSaveFileDocCommand;
- aSaveFileDocCommand->ISaveFileDocCommand(itsCommandNumber, this);
- aSaveFileDocCommand->fUseAppleEvent = TRUE; // This is the difference!
- this->PostCommand(aSaveFileDocCommand);
-
- TemporaryAllocation(oldTempAlloc);
- AllocateObjectsFromPerm(oldObjectPerm);
-
- fi.Success();
- }
- else // Recover
- {
- FreeIfObject(aSaveFileDocCommand);
- TemporaryAllocation(oldTempAlloc);
- AllocateObjectsFromPerm(oldObjectPerm);
-
- if (fi.error != userCanceledErr)
- fi.ReSignal();
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::SaveDocument:
- //----------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- void TFileBasedDocument::SaveDocument(CommandNumber itsCommandNumber)
- {
- Boolean askForFilename = (itsCommandNumber == cClose) && !fFileExists;
- Boolean makingCopy = itsCommandNumber == cSaveCopy;
- Boolean copyFInfo = fFileExists;
-
- if (copyFInfo)
- this->CheckFile(kIDBuzzString, bzSaveAnyways, FALSE);
-
- if (fCommitOnSave || (!makingCopy) )
- this->CommitLastCommand();
-
- SaveFile(itsCommandNumber, askForFilename, copyFInfo, makingCopy);
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::MakeCloseCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment MAClose
-
- TCloseDocCommand* TFileBasedDocument::MakeCloseCommand()
- {
- TCloseFileDocCommand* aCloseCommand = new TCloseFileDocCommand;
- aCloseCommand->ICloseFileDocCommand(cClose, this);
- return aCloseCommand;
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::FileHasBeenSaved:
- //----------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- void TFileBasedDocument::FileHasBeenSaved(const CStr255& newName)
- {
- this->SetChangeCount(0);
-
- if (fTitle != newName)
- this->SetTitle(newName);
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::SFPutParms:
- //----------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- void TFileBasedDocument::SFPutParms(CommandNumber itsCommandNumber,
- CStr255& prompt,
- CStr255& defaultName,
- ResNumber& dlgID,
- CPoint& where,
- ProcPtr& dlgHook,
- ProcPtr& modalFilter,
- Ptr& activeList,
- ProcPtr& activateProc,
- StandardFileReply* /*reply*/,
- void*& yourDataPtr)
- {
- short idx;
-
- dlgID = sfPutDialogID;
- where = kBestSystemLocation;
-
- switch (itsCommandNumber)
- {
- case cSave:
- case cSaveAs:
- idx = bzSaveAs;
- break;
-
- case cSaveCopy:
- idx = bzSaveCopy;
- break;
-
- default:
- idx = 0;
- break;
- }
-
- if (idx == 0)
- prompt.Empty();
- else
- GetIndString(prompt, kIDBuzzString, idx);
-
- this->GetTitle(defaultName);
-
- dlgHook = NULL;
- modalFilter = (ProcPtr) gModalFilterYDProcPtr;
- activeList = NULL;
- activateProc = NULL;
- yourDataPtr = NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::GetFileHandler:
- //----------------------------------------------------------------------------------------
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::SetTitle:
- //----------------------------------------------------------------------------------------
- #pragma segment MADocumentRes
-
- void TFileBasedDocument::SetTitle(const CStr255& aTitle)
- {
- Inherited::SetTitle(aTitle);
- SetFileName(aTitle);
- }
-
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::GetFinderMessage:
- //----------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- void TFileBasedDocument::GetFinderMessage(CStr255& theMessage)
- {
- theMessage.Empty();
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::MakeCloseCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment MAClose
-
- void TFileBasedDocument::DoClose(CommandNumber aCommand,
- Boolean useAppleEvent)
- {
- MAVolatileInit(Boolean, oldTempAlloc, TemporaryAllocation(TRUE));
- MAVolatileInit(Boolean, oldObjectPerm, AllocateObjectsFromPerm(FALSE));
- MAVolatileInit(TCloseFileDocCommand*, aCloseFileDocCommand, NULL);
-
- FailInfo fi;
- Try(fi)
- {
- aCloseFileDocCommand = new TCloseFileDocCommand;
- aCloseFileDocCommand->ICloseFileDocCommand(aCommand, this);
- aCloseFileDocCommand->fUseAppleEvent = useAppleEvent;
-
- this->PostCommand(aCloseFileDocCommand);
-
- TemporaryAllocation(oldTempAlloc);
- AllocateObjectsFromPerm(oldObjectPerm);
-
- fi.Success();
- }
- else // Recover
- {
- TemporaryAllocation(oldTempAlloc);
- AllocateObjectsFromPerm(oldObjectPerm);
- FreeIfObject(aCloseFileDocCommand);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // File Handling
- //----------------------------------------------------------------------------------------
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::FileAlreadyOpen:
- //----------------------------------------------------------------------------------------
- #pragma segment MADocumentRes
-
- Boolean TFileBasedDocument::FileAlreadyOpen(TFile* aFile)
- {
- if (fFileExists)
- return fFile->IsSameFile(aFile);
- else
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::FileChanged:
- //----------------------------------------------------------------------------------------
- #pragma segment MAFile
-
- OSErr TFileBasedDocument::FileChanged(Boolean checkType)
- {
- HParamBlockRec pb;
- OSErr err = noErr;
-
- if (fFileExists)
- {
- err = fFile->GetFileInfo(pb);
-
- if (err == noErr)
- {
- if (checkType && (pb.fileParam.ioFlFndrInfo.fdType != fFile->fFileType))
- err = errFTypeChanged;
- else if (fFile->IsModified())
- err = errFileChanged;
- }
- }
- return err;
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::FileExists:
- //----------------------------------------------------------------------------------------
- #pragma segment MAFile
-
- Boolean TFileBasedDocument::FileExists()
- {
- return fFileExists;
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::GetFile:
- //----------------------------------------------------------------------------------------
- #pragma segment MADocumentRes
-
- TFile* TFileBasedDocument::GetFile()
- {
- return fFile;
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::SetFile:
- //----------------------------------------------------------------------------------------
- #pragma segment MADocumentRes
-
- void TFileBasedDocument::SetFile(TFile* itsNewFile)
- {
- this->CloseFile();
- fFile = (TFile*)FreeIfObject(fFile);
- fFile = itsNewFile;
- fFileExists = FALSE; // Until we are shown otherwise
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::GetFileName:
- //----------------------------------------------------------------------------------------
- #pragma segment MADocumentRes
-
- void TFileBasedDocument::GetFileName(CStr63& aName)
- {
- if (fFile)
- fFile->GetName(aName);
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::GetSaveInfo:
- //----------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- Boolean TFileBasedDocument::GetSaveInfo(CommandNumber,
- Boolean copyFInfo,
- CInfoPBRec& cInfo)
- {
- Boolean saveInfo = FALSE;
-
- if (fFileExists && copyFInfo)
- {
- OSErr err = fFile->GetCatInfo(cInfo);
- if (err == noErr)
- saveInfo = TRUE;
- }
- // set the type and creator in case it has changed; the file might be on a file server
- // and someone else could have changed the document
- cInfo.hFileInfo.ioFlFndrInfo.fdType = fFile->fFileType;
- cInfo.hFileInfo.ioFlFndrInfo.fdCreator = fFile->fCreator;
- return saveInfo;
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::GetTempName:
- //----------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- void TFileBasedDocument::GetTempName(CStr63& filename)
- {
- const short maxName = 31; //maximum name size to generate
- const short maxNumber = 10; //maximum # digits of the random number
- const short maxPrefix = maxName - maxNumber;
-
- CStr255 name;
- unsigned long time;
-
- // If the file is untitled, use the application name.
- if (fFile)
- fFile->GetName((CStr63 &)name);
-
- if (name.IsEmpty())
- gDispatcher->GetApplicationName(name);
-
- filename = name;
-
- // Check prefix length, and trim it down if too large.
- if (filename.Length() > maxPrefix)
- filename.Length() = (char)maxPrefix;
-
- // Append a pseudo-random number.
- GetDateTime(&time);
- NumToString((time ^ (TickCount() >> 16)) & 0x7FFF, name); // don't have neg number
- filename += name;
-
- // Check name length, and trim it down if too large.
- if (filename.Length() > maxName)
- filename.Length() = (char)maxName;
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::ReadFile:
- //----------------------------------------------------------------------------------------
- #pragma segment MAReadFile
-
- void TFileBasedDocument::ReadFile(Boolean forPrinting)
- {
- if (fFile)
- {
- OSType itsType = NULL;
- FailInfo fi;
- Try(fi)
- {
- FailOSErr(fFile->GetFileType(itsType));
-
- if (itsType != kFolderType)
- {
- FailOSErr(fFile->OpenFile());
-
- // Just in case the file was left open, reset to the beginning of the file
- fFile->SetDataMark(0, fsFromStart);
- }
-
- fFileExists = TRUE;
-
- this->DoRead(fFile, forPrinting);
-
- fi.Success();
- }
- else // Recover
- {
- if (itsType != kFolderType)
- this->CloseFile();
- fi.ReSignal();
- }
-
- if (itsType != kFolderType)
- FailOSErr(fFile->CloseFileIfNotKeptOpen());
-
- fFile->Modified(); // Set the modification date
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::RequestFileName:
- //----------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- void TFileBasedDocument::RequestFileName(CommandNumber itsCommandNumber,
- Boolean /*makingCopy*/,
- TFile* aFile)
- {
- Boolean goodReply;
-
- #if !qPowerPC && !qModelCFM
- if (HasCustomFile())
- {
- #endif
- CStr255 prompt;
- CStr255 filename;
- short dlgID;
- CPoint dlgLoc;
- ProcPtr dlgHook;
- ProcPtr modalFilter;
- Ptr activeList;
- ProcPtr activateProc;
- void* yourDataPtr;
- StandardFileReply customReply;
-
- this->SFPutParms(itsCommandNumber, prompt, filename, dlgID, dlgLoc, dlgHook,
- modalFilter, activeList, activateProc, &customReply, yourDataPtr);
-
- gRsrcCheck = 0; //force immediate check
-
- FailOSErr(MAInteractWithUser());
-
- if (yourDataPtr == NULL)
- yourDataPtr = &itsCommandNumber;
-
- gClipboardMgr->AboutToLoseControl(TRUE); // so scrap gets converted
-
- DlgHookYDUPP cpfDlgHook = NewDlgHookYDProc(dlgHook);
- ModalFilterYDUPP cpfModalFilter = NewModalFilterYDProc(modalFilter);
- ActivateYDUPP cpfActivateProc = NewActivateYDProc(activateProc);
-
- CustomPutFile(prompt, filename, &customReply, dlgID, dlgLoc, cpfDlgHook, cpfModalFilter, (short*)activeList, cpfActivateProc, yourDataPtr);
-
- cpfDlgHook = (DlgHookYDUPP)DisposeIfRoutineDescriptor((UniversalProcPtr)cpfDlgHook);
- cpfModalFilter = (ModalFilterYDUPP)DisposeIfRoutineDescriptor((UniversalProcPtr)cpfModalFilter);
- cpfActivateProc = (ActivateYDUPP)DisposeIfRoutineDescriptor((UniversalProcPtr)cpfActivateProc);
-
- gClipboardMgr->RegainControl(TRUE); // so scrap gets converted
-
- goodReply = customReply.sfGood;
- if (goodReply)
- aFile->Specify(customReply.sfFile); // Don't use SpecifyWithStandardFile
- // here since that would clobber the
- // filetype.
- #if !qPowerPC && !qModelCFM
- }
- else
- {
- CStr255 prompt;
- CStr255 filename;
- short dlgID;
- CPoint dlgLoc;
- ProcPtr dlgHook;
- ProcPtr modalFilter;
- Ptr activeList;
- ProcPtr activateProc;
- void* yourDataPtr;
- SFReply reply;
-
- // We can't pass reply because it's a lowly SFReply, not a StandardFileReply
- this->SFPutParms(itsCommandNumber, prompt, filename, dlgID, dlgLoc, dlgHook,
- modalFilter, activeList, activateProc, NULL, yourDataPtr);
-
- gRsrcCheck = 0; //force immediate check
-
- FailOSErr(MAInteractWithUser());
-
- if (yourDataPtr == NULL)
- yourDataPtr = &itsCommandNumber;
-
- // We will pass the address of the CallBack instead of the modalFilter to
- // SFPPutFile It will add yourDataPtr parameter before passing on to the
- // ModalFilterProc supplied by SFPutParms this lets us assume a single calling
- // convention for that function.
-
- // Don't create a CallBack when the SF callback is NULL.
- // Also, pass itsCommandNumber as a default yourDataPtr.
-
- CallBack myModalHookCallBack;
- SetCallBack(dlgHook, (long)yourDataPtr, &myModalHookCallBack);
- DlgHookProcPtr aDlgHookProcPtr = dlgHook ? (DlgHookProcPtr)&myModalHookCallBack : NULL;
-
- CallBack myModalFilterCallBack;
- SetCallBack(modalFilter, (long)yourDataPtr, &myModalFilterCallBack);
- ModalFilterProcPtr aModalFilterProcPtr = modalFilter ? (ModalFilterProcPtr)&myModalFilterCallBack : NULL;
-
- gClipboardMgr->AboutToLoseControl(TRUE); // so scrap gets converted
-
- SFPPutFile(dlgLoc, prompt, filename, aDlgHookProcPtr, &reply, dlgID, aModalFilterProcPtr);
-
- gClipboardMgr->RegainControl(TRUE); // so scrap gets converted
-
- goodReply = reply.good;
- if (goodReply)
- FailOSErr(aFile->SpecifyWithTrio(reply.vRefNum, 0, reply.fName));
- }
- #endif
-
- if (goodReply)
- {
- // See if there is an open document with the same name. If there is, tell it
- // we're trying to save it again, which will ordinarily result in failure.
- TDocument* otherDoc = gDispatcher->FindDocument(aFile);
- if (otherDoc)
- otherDoc->SaveAgain(itsCommandNumber, this);
-
- // User has already confirmed deleting target in this case, so trash file and get
- // maximum disk space.
- OSErr err = aFile->DeleteFile();
- if ((err != noErr) && (err != fnfErr))
- Failure(err, 0);
- }
- else
- Failure(noErr, messageCancelled); // user cancelled
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::SaveFile:
- //----------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- void TFileBasedDocument::SaveFile(CommandNumber itsCommandNumber,
- Boolean askForFilename,
- Boolean copyFInfo,
- Boolean makingCopy)
- {
- MAVolatileInit(Boolean, volatileAskForFilename, askForFilename);
- //MAVolatileInit(Boolean, volatileMakingCopy, makingCopy); // can't be volatile because it is passed by reference
-
- long dataBytes = 0;
- long rsrcBytes = 0;
- long neededBlks;
- long usedBlks;
- long freeBlks;
- long blkSize;
- Boolean canSaveInPlace;
- CStr255 name;
- OSErr err;
- MAVolatile(Boolean, oldObjectPerm);
- MAVolatileInit(TFile*, theSaveFile, NULL);
-
- FailInfo fi;
- Try(fi)
- {
- // This can't fail or we won't be able to save in low memory conditions.
- oldObjectPerm = AllocateObjectsFromPerm(FALSE);
- theSaveFile = this->DoMakeFile(itsCommandNumber);
- AllocateObjectsFromPerm(oldObjectPerm);
-
- if (volatileAskForFilename)
- this->RequestFileName(itsCommandNumber, makingCopy, theSaveFile);
- else
- theSaveFile->SpecifyWithFile(fFile);
-
- theSaveFile->SetPermissions(fsRdWrPerm, fsRdWrPerm);// Since we intend to write to it
-
- this->AboutToSaveFile(theSaveFile, itsCommandNumber, makingCopy);
-
- // Get information about the volume saving to
- FailOSErr(theSaveFile->GetFreeBlocks(freeBlks));
-
- // Don't fill the disk completely.
- freeBlks--;
-
- // compute size needed to save document
- FailOSErr(theSaveFile->GetBlockSize(blkSize));
-
- this->DoNeedDiskSpace(theSaveFile, dataBytes, rsrcBytes);
- neededBlks = NumBlocks(rsrcBytes, blkSize) + NumBlocks(dataBytes, blkSize);
-
- // If there is enough space we're done. Save the file by the method specified
- if (freeBlks >= neededBlks)
- {
- if ((fHowToSave == svtAskUser) || (fHowToSave == svtAlways))
- this->SaveViaTemp(itsCommandNumber, makingCopy, copyFInfo, theSaveFile);
- else
- this->SaveInPlace(itsCommandNumber, makingCopy, copyFInfo, theSaveFile);
- }
- // Is there enough space to save the file in place?
- else
- {
- canSaveInPlace = FALSE; // Default value
-
- // See if we can save in place be recovering the space used by the existing file
- err = theSaveFile->GetPhysicalSize(dataBytes, rsrcBytes);
- if (err == noErr)
- {
- usedBlks = NumBlocks(dataBytes, blkSize) + NumBlocks(rsrcBytes, blkSize);
-
- if (neededBlks <= usedBlks + freeBlks)
- {
- // Saving in place purges the existing file. Should we ask before going ahead?
- if ((fHowToSave == svtAskUser) || (fHowToSave == sipAskUser))
- {
- FailOSErr(MAInteractWithUser());
- theSaveFile->GetName((CStr63 &)name);
- ParamText(name, gEmptyString, gEmptyString, gEmptyString);
- if (MacAppAlert(phPurgeOld, NULL) == kYesButton)
- canSaveInPlace = TRUE;
- else
- Failure(noErr, messageCancelled);
- }
- else if (fHowToSave == sipAlways)
- canSaveInPlace = TRUE;
- }
- }
- // If no file to recover space from, signal disk full error. Otherwise just display error
- else if (err != fnfErr)
- Failure(err, 0);
-
- if (canSaveInPlace)
- this->SaveInPlace(itsCommandNumber, makingCopy, copyFInfo, theSaveFile);
- else
- Failure(dskFulErr, 0);
- }
-
- #if qDebugMsg
- err = theSaveFile->GetPhysicalSize(dataBytes, rsrcBytes);
- if (err == noErr)
- {
- usedBlks = NumBlocks(dataBytes, blkSize) + NumBlocks(rsrcBytes, blkSize);
- if (usedBlks != neededBlks)
- {
- fprintf(stderr, "In TFileBasedDocument::SaveFile: DoNeedDiskSpace estimated disk space incorrectly.\n");
- fprintf(stderr, "estimated # disk blocks = %ld\n", neededBlks);
- fprintf(stderr, " actual # disk blocks = %ld\n", usedBlks);
- }
- }
- #endif
-
- if (!makingCopy)
- {
- theSaveFile->GetName((CStr63 &)name);
- this->FileHasBeenSaved(name);
-
- fFileExists = TRUE;
- fFile->SpecifyWithFile(theSaveFile);
- fFile->Modified();
- FailOSErr(fFile->OpenFileIfKeptOpen());
- }
-
- fi.Success();
- }
- else // Recover
- {
- // Make sure that we continue to allocate objects from the right place.
- AllocateObjectsFromPerm(oldObjectPerm);
-
- if (theSaveFile)
- {
- err = theSaveFile->FlushVolume();
- if (fi.message == 0)
- theSaveFile->GetName((CStr63 &)gErrorParm3);
- theSaveFile = (TFile *)FreeIfObject(theSaveFile);
- }
-
- long newMsg;
-
- if (!volatileAskForFilename)
- newMsg = messageSaveFailed;
- else if (makingCopy)
- newMsg = messageSaveCopyFailed;
- else
- newMsg = messageSaveAsFailed;
-
- FailNewMessage(fi.error, fi.message, newMsg);
- }
-
- err = theSaveFile->FlushVolume();
-
- theSaveFile->Free();
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::SaveInPlace:
- //----------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- void TFileBasedDocument::SaveInPlace(CommandNumber itsCommandNumber,
- Boolean makingCopy,
- Boolean copyFInfo,
- TFile* itsFile)
- {
- MAVolatileInit(TFile*, volatileItsFile, itsFile);
-
- CInfoPBRec cInfo;
- Boolean validInfo = this->GetSaveInfo(itsCommandNumber, copyFInfo, cInfo);
-
- this->CloseFile(); // close the file
-
- {
- OSErr err = volatileItsFile->DeleteFile();
- if ((err != noErr) && (err != fnfErr))
- Failure(err, 0);
- }
-
- FailInfo fi;
- Try(fi)
- {
- FailOSErr(volatileItsFile->CreateFile());
-
- if (validInfo) // Change attributes of volatileItsFile?
- FailOSErr(volatileItsFile->SetCatInfo(cInfo));
-
- FailOSErr(volatileItsFile->OpenFile());
-
- this->DoWrite(volatileItsFile, makingCopy);
-
- fi.Success();
- }
- else // Recover
- {
- OSErr recoverErr;
-
- recoverErr = volatileItsFile->CloseFile();
- #if qDebugMsg
- if (recoverErr != noErr)
- fprintf(stderr, "In SaveInPlace recovery: error from CloseFile is %d\n", recoverErr);
- #endif
-
- recoverErr = volatileItsFile->DeleteFile();
- #if qDebugMsg
- if ((recoverErr != noErr) && (recoverErr != fnfErr))
- fprintf(stderr, "In SaveInPlace recovery: error from DeleteFile is %d\n", recoverErr);
- #endif
-
- fi.ReSignal();
- }
-
- FailOSErr(volatileItsFile->CloseFile());
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::SaveViaTemp:
- //----------------------------------------------------------------------------------------
- #pragma segment MAWriteFile
-
- void TFileBasedDocument::SaveViaTemp(CommandNumber itsCommandNumber,
- Boolean makingCopy,
- Boolean copyFInfo,
- TFile* itsFile)
- {
- MAVolatileInit(TFile*, volatileItsFile, itsFile);
- MAVolatileInit(Boolean, volatileMakingCopy, makingCopy);
-
- CInfoPBRec cInfo;
- Boolean validInfo = this->GetSaveInfo(itsCommandNumber, copyFInfo, cInfo);
-
- FSSpec saveFileSpec;
- volatileItsFile->GetFileSpec(saveFileSpec); // Save file spec since it may be clobbered
-
- CStr63 tmpName; // not volatile...passed as const&
- this->GetTempName(tmpName);
-
- short tmpVRefNum;
- long tmpDirID;
- FailOSErr(FindFolder(saveFileSpec.vRefNum, kTemporaryFolderType, kCreateFolder, &tmpVRefNum, &tmpDirID));
- FailOSErr(volatileItsFile->SpecifyWithTrio(tmpVRefNum, tmpDirID, tmpName));
-
- FailInfo fi;
- Try(fi)
- {
- FailOSErr(volatileItsFile->CreateFile());
-
- if (validInfo) // Change attributes of volatileItsFile??
- FailOSErr(volatileItsFile->SetCatInfo(cInfo));
- FailOSErr(volatileItsFile->OpenFile());
-
- this->DoWrite(volatileItsFile, volatileMakingCopy);
-
- fi.Success();
- }
- else // Recover
- {
- OSErr recoverErr;
-
- recoverErr = volatileItsFile->CloseFile();
-
- #if qDebugMsg
- if (recoverErr != noErr)
- fprintf(stderr, "In SaveViaTemp error recovery: error from CloseFile is %d\n", recoverErr);
- #endif
-
- recoverErr = volatileItsFile->DeleteFile();
-
- #if qDebugMsg
- if ((recoverErr != noErr) && (recoverErr != fnfErr))
- fprintf(stderr, "In SaveViaTemp error recovery: error from volatileItsFile.DeleteFile is %d\n", recoverErr);
- #endif
-
- fi.ReSignal();
- }
-
- FailOSErr(volatileItsFile->CloseFile());
-
- if (!volatileMakingCopy)
- this->CloseFile(); // Close the documents file
-
- Try(fi)
- {
- if (fFileExists && copyFInfo)
- {
- // Since ExchangeFiles doesn't check that the destination file is locked
- // we need to check ourselves.
- CInfoPBRec cInfo;
-
- BlockSet((Ptr) & cInfo, sizeof(CInfoPBRec), 0x00);// zero-out our storage
- FailOSErr(fFile->GetCatInfo(cInfo));
-
- if (cInfo.hFileInfo.ioFlAttrib & 0x01)
- FailOSErr(fLckdErr);
-
- // Exchange the existing file for the one in the temporary folder…
- FailOSErr(volatileItsFile->ExchangeFiles(fFile));
-
- // and delete the one left in the temporary folder
- OSErr err = volatileItsFile->DeleteFile();
- if ((err != noErr) && (err != fnfErr))
- Failure(err, 0);
- }
- else
- // The temp file was created in the temporary directory.
- // Move it back where it belongs
- FailOSErr(volatileItsFile->MoveAndRename(saveFileSpec));
-
- // Specify the file again since it may have changed if it was saved in a temporary folder
- volatileItsFile->Specify(saveFileSpec);
-
- fi.Success();
- }
- else // Recover
- {
- OSErr recoverErr = volatileItsFile->DeleteFile();
-
- #if qDebugMsg
- if ((recoverErr != noErr) && (recoverErr != fnfErr))
- fprintf(stderr, "In SaveViaTemp error recovery: error from DeleteFile is %d\n", recoverErr);
- #endif
-
- if (fFileExists && !volatileMakingCopy)
- recoverErr = fFile->OpenFileIfKeptOpen();
-
- #if qDebugMsg
- if (recoverErr != noErr)
- fprintf(stderr, "In SaveViaTemp error recovery: error from OpenFileIfKeptOpen is %d\n", recoverErr);
- #endif
-
- // Set the name to something intelligent in case we have to display it to the user.
- volatileItsFile->SetName(saveFileSpec.name);
-
- fi.ReSignal();
- }
-
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::SetFileName:
- //----------------------------------------------------------------------------------------
- #pragma segment MADocumentRes
-
- void TFileBasedDocument::SetFileName(const CStr63& aName)
- {
- if (fFile)
- fFile->SetName(aName);
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::ValidateFileSpec:
- //----------------------------------------------------------------------------------------
- #pragma segment MADocumentRes
-
- Boolean TFileBasedDocument::ValidateFileSpec()
- {
- return (fFile != NULL) ? fFile->UpdateFileSpec() : FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // Scripting
- //----------------------------------------------------------------------------------------
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::CountContainedObjects:
- //----------------------------------------------------------------------------------------
- #pragma segment OSLDispatchRes
-
- long TFileBasedDocument::CountContainedObjects(DescType desiredType)
- {
- if (desiredType == cFile)
- {
- if (this->FileExists())
- return 1;
- else
- return 0;
- }
- else
- return TDocument::CountContainedObjects(desiredType);
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::GetContainedObject:
- //----------------------------------------------------------------------------------------
- #pragma segment OSLDispatchRes
-
- MScriptableObject* TFileBasedDocument::GetContainedObject(DescType desiredType,
- DescType selectionForm,
- const CAEDesc& selectionData)
- {
- MScriptableObject * result = NULL;
- if (desiredType == cFile)
- {
- if (this->FileExists())
- {
- if (selectionForm == formName)
- {
- TFile * resultFile = this->DoMakeFile(cOpen);
- CStr255 thePathName;
- selectionData.GetString(thePathName);
- resultFile->SpecifyWithTrio(0, 0, (CStr63 &)thePathName);
- TOSADispatcher::fgDispatcher->AddTemporaryToken(resultFile);
- result = resultFile;
- }
- else
- {
- TFile * myFile = GetFile();
- if (myFile)
- {
- TFile * resultFile = this->DoMakeFile(cOpen);
- resultFile->SpecifyWithFile(myFile);
- TOSADispatcher::fgDispatcher->AddTemporaryToken(resultFile);
- result = resultFile;
- }
- }
- }
- }
- else
- result = Inherited::GetContainedObject(desiredType, selectionForm, selectionData);
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // Container Application Support
- //----------------------------------------------------------------------------------------
-
- #if qContainer
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::MakeDatedUniqueFSSpec - Static
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- void TFileBasedDocument::MakeDatedUniqueFSSpec(FSSpec* fsSpec)
- {
- static short index = 1;
-
- CStr32 tmpName(fsSpec->name);
-
- {
- CStr32 dateString;
- unsigned long secs;
-
- GetDateTime(&secs);
- DateString(secs, shortDate, dateString, NULL);
-
- // ••• MDR
- #if 1
- tmpName = tmpName + " " + dateString;
- tmpName = tmpName + " ";
- #else
- tmpName = tmpName + "\p " + dateString;
- tmpName = tmpName + "\p ";
- #endif
- }
-
- FInfo ignore;
-
- do
- {
- CStr32 indexString;
- NumToString(index, indexString);
-
- CStr32(tmpName + indexString).CopyTo(fsSpec->name);
- ++index;
-
- } while (FSpGetFInfo(fsSpec, &ignore) == noErr);
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::GetContainer:
- //----------------------------------------------------------------------------------------
- #pragma segment MADocumentRes
-
- CADocumentRef TFileBasedDocument::GetContainer()
- {
- return fContainerDocument;
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::AddODPartView:
- //----------------------------------------------------------------------------------------
- #pragma segment MADocumentRes
-
- void TFileBasedDocument::AddODPartView(TODPartView* aNewODPartView)
- {
- if (fODPartViewList)
- fODPartViewList->Insert((TObject*)aNewODPartView);
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::DeleteODPartView:
- //----------------------------------------------------------------------------------------
- #pragma segment MADocumentRes
-
- void TFileBasedDocument::DeleteODPartView(TODPartView* partViewToRemove)
- {
- if (fODPartViewList)
- fODPartViewList->Delete((TObject*)partViewToRemove);
- }
-
- //----------------------------------------------------------------------------------------
- // TFileBasedDocument::RegisterCAHandlers:
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- void TFileBasedDocument::RegisterCAHandlers()
- {
- if (fContainerDocument)
- {
- CAInstallFocusNotification(
- TDispatcher::FocusAcquiredProc,
- TDispatcher::FocusLostProc,
- fContainerDocument );
- FailOSErr(CAError());
-
- CAInstallWindowActivateHandler(
- TDispatcher::WindowActivateHandler,
- fContainerDocument );
- FailOSErr(CAError());
-
- CAInstallFrameShapeRequestHandler(
- TDispatcher::FrameShapeRequestHandler,
- fContainerDocument );
- FailOSErr(CAError());
-
- CAInstallBorderAdjuster(
- fContainerDocument,
- TDispatcher::AdjustBorderHandler );
- FailOSErr(CAError());
- }
- }
-
- #endif
-
- //----------------------------------------------------------------------------------------
- // End of UFileBasedDocument.cp
-
- #pragma segment Inline
-